#ifndef NCSPROPTREE_H
#define NCSPROPTREE_H

#include <ncsppt_export.h>
#include "ncspropmgr.h" //! @todo build NcsPropBrowser module, put editor creator function into this module. 
#include <QTreeWidget>

class NcsPropMgr; 
class NcsProp;
class QMouseEvent;
class QKeyEvent;

class NcsPropTreePrivate; 

class NCSPPT_EXPORT NcsPropTree : public QTreeWidget
{
    Q_OBJECT

public:
    NcsPropTree(QWidget* parent = nullptr); 
    virtual ~NcsPropTree(); 

    enum ClearOption
    {
        CLEAR_PROP    = 1 << 0, // items and NcsProp will be cleared.  
        CLEAR_FACTORY = 1 << 1, 
    };

    void clearPropTree(uint32_t clear_options = CLEAR_PROP | CLEAR_FACTORY);

    void updatePropView(NcsPropMgr* prop_mgr);

    using QTreeWidget::itemFromIndex;

	QTreeWidgetItem* itemFromProperty(NcsProp* prop) const;

	void setBackgroundColor(QTreeWidgetItem *item, const QColor &color);
	//QColor backgroundColor(QTreeWidgetItem *item) const;
	//QColor calculatedBackgroundColor(QTreeWidgetItem *item) const;

	void setMarkNullProp(bool mark);
	//bool propertiesWithoutValueMarked() const;

    /** 设置prop或者type的编辑器工厂函数, 与NcsPropMgr中不同的是，这里的工厂创建出来的
        widget不会响应编辑信号，而是直接成为item的widget. 

        参见Bool类型, QColor类型， QFont类型在tree中的显示和行为. 
        这个功能参考了QtPropertyTreeBrowser中对bool值, qcolor , qfont的显示. 
    */
    void setPropEditorFactory(NcsProp* prop, NcsPropMgr::EditorFn editor_factory);
    void setTypeEditorFactory(int prop_type, NcsPropMgr::EditorFn editor_factory);

protected:
    void initWidget();
    virtual void drawRow(QPainter *painter, const QStyleOptionViewItem &option, 
                 const QModelIndex &index) const override;

    virtual void mousePressEvent(QMouseEvent *event) override; 
    virtual void keyPressEvent(QKeyEvent *event) override; 
    QTreeWidgetItem* addProp(NcsProp* prop, NcsProp* parent); // for private use only

protected:
    friend class NcsPropTreePrivate;
    NcsPropTreePrivate* d = nullptr; 
    NcsPropMgr* m_propmgr = nullptr; // keep current prop manager for checking  errors 
    QMap<NcsProp*, QTreeWidgetItem*> m_prop2item;
};

#endif // NCSPROPTREE_H
